// m

// 给定一棵二叉树，按照从顶部到底部的顺序，返回从右侧能看到的节点值

// 解题思路：二叉树的层序遍历，不过遍历每层节点的时候，只需要将最后一个节点加入结果数组即可

function rightSideView(root) {
    if (!root) {
        return []
    }
    let queue = [root]
    let order = []
    while (queue.length) {
        let level = []
        let size = queue.length
        for (let i = 0; i < size; i++) {
            let curr = queue.shift()
            level.push(curr.val)
            if (curr.left) {
                queue.push(curr.left)
            }
            if (curr.right) {
                queue.push(curr.right)
            }
        }
        order.push(level[level.length - 1])
    }
    return order
}

const root = {
    val: "A",
    left: {
        val: "B",
        left: {
            val: "D",
        },
        right: {
            val: "E",
        },
    },
    right: {
        val: "C",
        right: {
            val: "F",
        },
    },
};

console.log(rightSideView(root));
